package com.qianguo.buyer.widget.multiwheelpicker;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.eva.androidlibrary.widget.wheelpicker.core.AbstractWheelPicker;
import com.eva.androidlibrary.widget.wheelpicker.view.WheelCrossPicker;
import com.eva.androidlibrary.widget.wheelpicker.view.WheelCurvedPicker;

/**
 * @author eva
 * @date 16/6/6
 */
public class MultiWheelPicker extends LinearLayout {

    protected static final int GROUP = 0;
    protected static final int CHILD = 1;

    protected WheelCurvedPicker groupPicker, childPicker;
    protected MultiWheelAdapter adapter;
    protected int groupIndex, childIndex;
    protected String groupData, childData;
    protected OnMultiWheelChangeListener listener;
    protected float labelTextSize;

    public MultiWheelPicker(Context context) {
        super(context);
    }

    public MultiWheelPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MultiWheelPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnMultiWheelChangeListener(OnMultiWheelChangeListener listener) {
        this.listener = listener;
    }

    public void setAdapter(MultiWheelAdapter adapter) {
        this.adapter = adapter;

        setGravity(Gravity.CENTER);
        setOrientation(HORIZONTAL);
        int padding = getResources().getDimensionPixelSize(com.eva.androidlibrary.R.dimen.WheelPadding);
        int padding2x = padding * 2;
        labelTextSize = padding;
        LayoutParams llParams = new LayoutParams(-2, -2);

        groupPicker = new WheelCurvedPicker(getContext());
        childPicker = new WheelCurvedPicker(getContext());

        groupPicker.setPadding(0, padding, padding2x, padding);
        childPicker.setPadding(0, padding, padding2x, padding);

        groupPicker.setData(adapter.getGroupData());

        initListener(groupPicker, GROUP);
        initListener(childPicker, CHILD);

        addView(groupPicker, llParams);
        addView(childPicker, llParams);

    }

    private void initListener(WheelCrossPicker picker, final int wheelIndex) {
        picker.setOnWheelChangeListener(new AbstractWheelPicker.OnWheelChangeListener() {
            @Override
            public void onWheelScrolling(float deltaX, float deltaY) {
                if (listener != null) {
                    if (wheelIndex == GROUP) {
                        listener.onGroupWheelScrolling(deltaX, deltaY);
                    } else {
                        listener.onChildWheelScrolling(deltaX, deltaY);
                    }
                }
            }

            @Override
            public void onWheelSelected(int index, String data) {
                if (wheelIndex == GROUP) {
                    groupIndex = index;
                    groupData = data;
                    childPicker.setData(adapter.getChildData(index));
                    childPicker.setItemIndex(0);
                } else {
                    childIndex = index;
                    childData = data;
                }

                if (listener != null) {
                    listener.onWheelSelected(groupIndex, childIndex, groupData, childData);
                }
            }

            @Override
            public void onWheelScrollStateChanged(int state) {

            }
        });

    }

}
